//
//  Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
//  Use of this source code is subject to the terms of the Microsoft end-user
//  license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
//  If you did not accept the terms of the EULA, you are not authorized to use
//  this source code. For a copy of the EULA, please see the LICENSE.RTF on your
//  install media.
//
// Portions Copyright (c) MiTAC Corp. All rights reserved.
//------------------------------------------------------------------------------
//
//  File:  lcdterm.c
//  Programmer: Visual.Wei
//  Date: 2007-4-16
//
//  Provide simple terminal like debug output on LCD graphics frame buffer
//  
//

#include <windows.h>
#include <bsp.h>
#include <bsp_oaldisp.h>
#include <bsp_oalkeys.h>
#include "lcd.h"
#include "display.h"
#include <s3c2443.h>


void *InitDisplay(void)
{
#if 1
	UINT8 pagewidth_in_byte=0,offsize_in_byte=0;	
        volatile S3C2443_IOPORT_REG *s2443IOP = (S3C2443_IOPORT_REG *)OALPAtoVA(S3C2443_BASE_REG_PA_IOPORT, FALSE);
	volatile S3C2443_LCD_REG    *s2443LCD = (S3C2443_LCD_REG *)OALPAtoVA(S3C2443_BASE_REG_PA_LCD, FALSE);
	UINT8 clkval=0;
	UINT16 hcync_cnt,vclk_cnt;
	UINT16 lcd_horizon_value,lcd_line_value;
	UINT8 lcd_vbpd,lcd_vfpd,lcd_vspw,lcd_hbpd,lcd_hfpd,lcd_hspw;
	UINT8 lcd_frame_rate;	
	//UINT16 * pFB = NULL;
    
	EdbgOutputDebugString("++InitDisplay\r\n");
	// Set up the LCD controller registers to display a power-on bitmap image.
	//
	s2443IOP->MISCCR |= (1<<28);  // select LCD controller for TFT lcd controller
	
	//s2443IOP->GPCUDP     = 0xFFFFFFFF;
	s2443IOP->GPCCON    = 0xAAAAAAA9;
	                            
	//s2443IOP->GPDUDP     = 0xFFFFFFFF;
	s2443IOP->GPDCON     = 0xAAAAAAAA; 
	
	//DM_SerialPrintf("LCD_PWR_ON# LOW");
	
	s2443IOP->GPACDH &= ~(1<<1);
	
	//DM_WaitMs(1);
	
	//rGPACDH &= ~(1<<0);
	//SetGPIOLevel(GPIO_LCD_ON, GPIO_LOW);//changes 0108
	//SetGPIOFunction(GPIO_LCD_ON,GPIO_OUTPUT); 
	//DM_WaitMs(1);

	lcd_horizon_value = HOZVAL_TFT;
	lcd_line_value = LINEVAL_TFT;
	lcd_vbpd = VBPD;
	lcd_vfpd = VFPD;
	lcd_vspw = VSPW;
	lcd_hbpd = HBPD;
	lcd_hfpd = HFPD;
	lcd_hspw = HSPW;
	lcd_frame_rate = LCD_FRAMERATE;//60;

	pagewidth_in_byte = lcd_horizon_value/8*16;		
	offsize_in_byte = 0;
	
	//LcdWindowOnOff(LCD_WIN_ALL,LCD_OFF);	
	s2443LCD->WINCON0 &= ~0x01;
	s2443LCD->WINCON1 &= ~0x01;
	
	//LcdEnvidOnOff(LCD_OFF);
	s2443LCD->VIDCON0 &= (~3); // ENVID Off using Per Frame method

	//Basic_Display_Setting(window,WINCONx_16BPP_565,lcd_horizon_value,0);
	
	s2443LCD->VIDCON0 =	(0 << 25) |
				VIDCON0_S_RGB_IF |
				(0 < 19) |
				(0 < 16) |
				VIDCON0_S_RGB_PAR |
				VIDCON0_S_CLKVAL_F_AlWAYS_UPDATE |
				VIDCON0_S_VCLK_GATING_OFF |
				VIDCON0_S_CLKDIR_DIVIDED |
				VIDCON0_S_CLKSEL_HCLK;
					
	hcync_cnt = (lcd_vbpd+lcd_vfpd+lcd_vspw+lcd_line_value);	
	vclk_cnt = (lcd_hbpd+lcd_hfpd+lcd_hspw+lcd_horizon_value);

//EdbgOutputDebugString("total:0x%x\r\n", (UINT8)(((float)S3C2443_HCLK/(float)(hcync_cnt*vclk_cnt*lcd_frame_rate))+0.5)-1);	
	clkval = (UINT8)(((float)133333333/(float)(hcync_cnt*vclk_cnt*lcd_frame_rate))+0.5)-1;
//	clkval = (UINT8)((float)133333333/(float)(325*285*60));	

	//DM_SerialPrintf("CLKVAL=%d",clkval);
	//DM_SerialPrintf("HCLK=%d",HCLK);
	
	s2443LCD->VIDCON0 |= (clkval <<VIDCON0_CLKVAL_F_SHIFT);

	//check point
	s2443LCD->VIDCON1 =	VIDCON1_S_VCLK_FALL_EDGE_FETCH |
				VIDCON1_S_HSYNC_INVERTED |
				VIDCON1_S_VSYNC_INVERTED;

	//DM_SerialPrintf("VIDCON0=0x%08X",rVIDCON0);
	//DM_SerialPrintf("VIDCON1=0x%08X",rVIDCON1);
	
	s2443LCD->VIDTCON0=((lcd_vbpd)<<VIDTCON0_BPD_S)|((lcd_vfpd)<<VIDTCON0_FPD_S)|(lcd_vspw);
	s2443LCD->VIDTCON1=((lcd_hbpd)<<VIDTCON0_BPD_S)|((lcd_hfpd)<<VIDTCON0_FPD_S)|(lcd_hspw);
	s2443LCD->VIDTCON2 = ((lcd_line_value)<<VIDTCON2_LINEVAL_S)|(lcd_horizon_value);
	
	s2443LCD->WINCON0 = (0<<WINCON_SWAP_S)|(WINCONx_4WORD_BURST<<WINCON_BURSTLEN_S)|(WINCONx_16BPP_565<<WINCON_BPP_S); // 4word burst, 16bpp, 
// james deng 02052007
	// rWINCON0 = (0<<WINCON_SWAP_S)|(WINCONx_4WORD_BURST<<WINCON_BURSTLEN_S)|(WINCONx_18BPP_666<<WINCON_BPP_S); // 4word burst, 16bpp, 
	// james deng 02052007

	s2443LCD->VIDOSD0A = (0<<VIDOSDxAB_HORIZON_X_S)|(0);
	s2443LCD->VIDOSD0B = ((lcd_horizon_value)<<VIDOSDxAB_HORIZON_X_S)|(lcd_line_value);	

	s2443LCD->VIDW00ADD0B0 = (UINT32)IMAGE_FRAMEBUFFER_DMA_BASE;		
	// buffer end address
	s2443LCD->VIDW00ADD1B0 = (UINT32)IMAGE_FRAMEBUFFER_DMA_BASE + (MIC_LCD_XSIZE_TFT*MIC_LCD_YSIZE_TFT*4);
	// buffer size 
	s2443LCD->VIDW00ADD2B0 = (offsize_in_byte<<VIDWxADD2_OFFSET_SIZE_S)|(MIC_LCD_XSIZE_TFT*4);

	s2443LCD->WIN0MAP = 0;
	
	s2443LCD->WINCON0 |= (1<<WINCON_SWAP_S);

	//SetGPIOLevel(GPIO_LCD_PWR_ON, GPIO_LOW);
	//SetGPIOFunction(GPIO_LCD_PWR_ON, GPIO_OUTPUT);
	//SetGPIOPullupDown(GPIO_LCD_PWR_ON, GPIO_ENABLE);

	#if 1
	s2443IOP->GPLCON = (s2443IOP->GPLCON & ~(0x3<<22)) | (0x1<<22); // Backlight Pwm control
	s2443IOP->GPLDAT = s2443IOP->GPLDAT & (0<<11);
	s2443IOP->GPLUDP = (s2443IOP->GPLUDP & ~(0x3<<22)) | (0x1<<22);
	#endif	

	//DM_WaitMs(5);
	EdbgOutputDebugString("LCD Controller ON\r\n");
	s2443LCD->WINCON0 |= 0x1;
	s2443LCD->VIDCON0 |= 0x3;  

	EdbgOutputDebugString("LCD_PON ON\r\n");
	//DM_WaitMs(5);	
	//rGPACDH = (rGPACDH & ~0x3) | (0x1);
	
	//SetGPIOLevel(GPIO_LCD_ON, GPIO_HIGH); //changes 0108
	//SetGPIOFunction(GPIO_LCD_ON, GPIO_OUTPUT);
	#if 0
	//willson059 start
	//s2443IOP->GPACDH = (s2443IOP->GPACDH & ~(0x3<<0)) | (0x1<<0); // Backlight Pwm control
	//s2443IOP->GPACDH = s2443IOP->GPACDH & (0<<1);
	//willson0509 end
	#endif
	//DM_WaitMs(170);
	EdbgOutputDebugString("Backlight ON\r\n");
	
	//SetGPIOLevel(GPIO_BL_PWM, GPIO_HIGH);
	//SetGPIOPullupDown(GPIO_BL_PWM, GPIO_ENABLE);
	//SetGPIOFunction(GPIO_BL_PWM, GPIO_OUTPUT);
	#if 1
	s2443IOP->GPBCON = (s2443IOP->GPBCON & ~(0x3<<0)) | (0x1<<0); // Backlight Pwm control
	s2443IOP->GPBDAT = s2443IOP->GPBDAT | (0x1<<0);
	s2443IOP->GPBUDP = (s2443IOP->GPBUDP & ~(0x3<<0)) | (0x01<<0);	
	#endif
	//changes 0108 for PT310
	

#endif	
	EdbgOutputDebugString("--InitDisplay\r\n");
	return (void *)(IMAGE_FRAMEBUFFER_DMA_BASE);
}

